const moment = require("moment")

// pages/cinemaDetail/cinemaDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperdata: [],
    // 当前轮播下标
    cinemaid: 0,
    nowindex: 0,
    CinemaDetail: {},
    CinemaPlan: [],
    //  filmId:"6405",
    MoviePlan: [],
    filmid: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //console.log(options)
    const cinemaid = options.id
    this.setData({
      cinemaid: cinemaid
    })
    this.getCinemaDetail(cinemaid)
    // 处理异步
    this.getCinemaPlan(cinemaid).then(data => {
      // console.log(data[this.data.nowindex])
      const filmid = data[this.data.nowindex].filmId
      this.setData({
        filmid: filmid
      })
      this.getMoviePlan(cinemaid, filmid)
    })
  },
  // 轮播图的改变
  swiperChange: function (event) {
    this.setData({
      nowindex: event.detail.current
    })
    const CinemaPlan = this.data.CinemaPlan
    const filmId = CinemaPlan[this.data.nowindex].filmId
    this.getMoviePlan(this.data.cinemaid, filmId)
  },
  // 获取详情方法
  getCinemaDetail: function (id) {
    wx.request({
      url: 'https://api.iynn.cn/film/api/v1/getCinemaInfo?cinemaId=' + id,
      success: (result) => {
        //  console.log(result.data.data.cinema)
        this.setData({
          CinemaDetail: result.data.data.cinema
        })
      },
    })
  },
  // 获取影院排片
  getCinemaPlan: function (id) {
    return new Promise((resole, reject) => {
      wx.request({
        url: 'https://api.iynn.cn/film/api/v1/getCinemaFilmsArrangement?cinemaId=' + id,
        success: (result) => {
          //console.log(result)
          const res = result.data.data.films
          resole(res)
          this.setData({
            CinemaPlan: res,
          })
        },
      })
    })
  },
  gotoDetail: function (e) {
    const filmid = e.currentTarget.dataset.filmid
    wx.navigateTo({
      url: '/pages/movieDetail/movieDetail?id=' + filmid,
    })
  },
  gotoMessage:function(e){
    //console.log(e.currentTarget.dataset.id)
    const cinemaid = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/cinemaService/cinemaService?id=' + cinemaid,
    })
  },
  // 一个电影的排片计划
  getMoviePlan: function (cinemaId, filmId) {
    wx.request({
      url: 'https://api.iynn.cn/film/api/v1/getCinemaFilmSchedules?cinemaId=' + cinemaId + '&filmId=' + filmId,
      success: (result) => {
        //console.log(result.data.data.schedules)
        const res = result.data.data.schedules
        res.forEach(item => {
          //console.log(item.showAt)
          item.showAt = moment.unix(item.showAt).format("HH:mm")
          item.endAt = moment.unix(item.endAt).format("HH:mm")
          //console.log(item.showAt,item.endAt)
        });
        //console.log(res)
        this.setData({
          MoviePlan: res
        })
      },
    })
  },
  makeCall:function(e){
    console.log(e)
    let phone=e.currentTarget.dataset.number
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  getLocation:function(e){
    wx.openLocation({
      latitude: e.currentTarget.dataset.latitude,
      longitude: e.currentTarget.dataset.longitude,
      address: e.currentTarget.dataset.address,
      name: e.currentTarget.dataset.name,
      scale: 18,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})